package com.uzai.console.service.tb.tbstoreblacklist.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.common.utils.EsIndexName;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPubIdDto;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPubQuery;
import com.uzai.console.entity.StoreBlacklistPub;
import com.uzai.console.entity.StoreBlacklistPubWhite;
import com.uzai.console.entity.es.WechatBlacklistPubWhite;
import com.uzai.console.mapper.StoreBlacklistPrvMapper;
import com.uzai.console.mapper.StoreBlacklistPubMapper;
import com.uzai.console.mapper.StoreBlacklistPubWhiteMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.tb.tbstoreblacklist.StoreBlacklistPubService;
import com.uzai.console.vo.tb.tbstoreblacklist.StoreBlacklistPubInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class StoreBlacklistPubServiceImpl implements StoreBlacklistPubService {

    @Autowired
    private StoreBlacklistPubMapper storeBlacklistPubMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private StoreBlacklistPrvMapper storeBlacklistPrvMapper;
    @Autowired
    private StoreBlacklistPubWhiteMapper storeBlacklistPubWhiteMapper;

    /**
     * 查询共享黑名单列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPubQuery
     * @return null
     */
    public Page<StoreBlacklistPubInfoVo> findStoreBlacklistPubList(StoreBlacklistPubQuery storeBlacklistPubQuery){
        //新建返回到页面的LIST对象
        List<StoreBlacklistPubInfoVo> storeBlacklistPubPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<StoreBlacklistPub> page = new Page<>();
        storeBlacklistPubQuery.convert(page);
        ArrayList<StoreBlacklistPub> storeBlacklistPubList =  storeBlacklistPubMapper.findByList(page, storeBlacklistPubQuery);
        if(storeBlacklistPubList != null && storeBlacklistPubList.size() > 0){
            for (StoreBlacklistPub storeBlacklistPub : storeBlacklistPubList){
                StoreBlacklistPubInfoVo storeBlacklistPubPageVo = new StoreBlacklistPubInfoVo();
                BeanUtils.copyProperties(storeBlacklistPub, storeBlacklistPubPageVo);

                //是否加入白名单
                StoreBlacklistPubWhite storeBlacklistPubWhite = storeBlacklistPubWhiteMapper.selectByStoreIdAndMerId(storeBlacklistPub.getStoreId(), storeBlacklistPubQuery.getMerId());
                if(storeBlacklistPubWhite == null){
                    storeBlacklistPubPageVo.setWhiteStatus(0);  //未加入白名单
                }else{
                    storeBlacklistPubPageVo.setWhiteStatus(1);  //已经加入白名单
                }

                storeBlacklistPubPageVoList.add(storeBlacklistPubPageVo);
            }
        }
        //返回到页面分页对象
        Page<StoreBlacklistPubInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(storeBlacklistPubPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 加入白名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPubIdDto
     * @return null
     */
    @Transactional
    public void addStoreBlacklistPubWhite(StoreBlacklistPubIdDto storeBlacklistPubIdDto){

        if(storeBlacklistPubIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectById(storeBlacklistPubIdDto.getId());

        if(storeBlacklistPub == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此全网黑名单不存在");
        }

        if(Tools.getLong(storeBlacklistPub.getMerId()).longValue() == storeBlacklistPubIdDto.getMerId().longValue()){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "不能将自己共享的全网黑名单加入到白名单当中");
        }

        StoreBlacklistPubWhite storeBlacklistPubWhite = storeBlacklistPubWhiteMapper.selectByStoreIdAndMerId(storeBlacklistPub.getStoreId(), storeBlacklistPubIdDto.getMerId());
        if(storeBlacklistPubWhite == null){
            //加入白名称
            storeBlacklistPubWhite = new StoreBlacklistPubWhite();
            BeanUtils.copyProperties(storeBlacklistPub, storeBlacklistPubWhite);
            storeBlacklistPubWhite.setId(IdWorker.getId());
            storeBlacklistPubWhite.setMerId(storeBlacklistPubIdDto.getMerId());
            Integer now = DateUtil.getNowTime();
            storeBlacklistPubWhite.setCreateTime(now);
            storeBlacklistPubWhite.setUpdateTime(now);
            storeBlacklistPubWhiteMapper.insert(storeBlacklistPubWhite);
        }else{
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此全网黑名单店铺id已经存在白名单列表中");
        }
    }

    /**
     * 删除白名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPubIdDto
     * @return null
     */
    @Transactional
    public void deleteStoreBlacklistPubWhite(StoreBlacklistPubIdDto storeBlacklistPubIdDto){

        if(storeBlacklistPubIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectById(storeBlacklistPubIdDto.getId());

        if(storeBlacklistPub == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此共享黑名单不存在");
        }

        StoreBlacklistPubWhite storeBlacklistPubWhite = storeBlacklistPubWhiteMapper.selectByStoreIdAndMerId(storeBlacklistPub.getStoreId(), storeBlacklistPubIdDto.getMerId());

        if(storeBlacklistPubWhite != null){
            storeBlacklistPubWhiteMapper.deleteById(storeBlacklistPubWhite.getId(), storeBlacklistPubIdDto.getMerId());
        }

    }

}
